//------------------------------------------------------------------------------
// Copyright (c) 2012 by Silicon Laboratories.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Silicon Laboratories End User
// License Agreement which accompanies this distribution, and is available at
// http://developer.silabs.com/legal/version/v10/License_Agreement_v10.htm
// Original content and implementation provided by Silicon Laboratories.
//------------------------------------------------------------------------------

//==============================================================================
// WARNING:
//
// This file is auto-generated by AppBuilder and should not be modified.
// Any hand modifications will be lost if the project is regenerated.
//==============================================================================
// library
#include <stdio.h>
// hal
#include <si32_device.h>

// application
#include "gModes.h"
#include "myUart0.h"
#include "myCpu.h"

//==============================================================================
// myApplication.
#include "SI32_PBSTD_A_Type.h"
#include <SI32_SPI_B_Type.h>
#include "bsp.h"
//==============================================================================

/*for part info*/
#define GET_PARTINFO {0x01}
U8 Get_pinf[1] = GET_PARTINFO;
U8* getpartinf_ptr = &Get_pinf[0];

U8 partarray[26];
U8* partarray_ptr = &partarray[0];
/*END for interrupt getting*/

/*get interrupt status */
#define GET_ITSTAT {0x20,0x00,0x00,0x00}
U8 Get_itstat[4] = GET_ITSTAT;
U8* Get_itstat_ptr = &Get_itstat[0];
/*END get interrupt status */

U8 getNumber_fromUART(void);

U8 getNumber_fromUART(){
	U8 data_in;
	   data_in = myUART0_get_char()-0x30;
	   data_in <<=4;
	   data_in += myUART0_get_char()-0x30;
	   return data_in;
}


U8 Power_up[7] = RF_POWER_UP;
U8* PWR_ptr = &Power_up[0];

U8 Gpio_pin_cfg[7] = RF_GPIO_PIN_CFG;
U8* Gpio_ptr = &Gpio_pin_cfg[0];

U8 Global_xo_tune[5] = RF_GLOBAL_XO_TUNE_1;
U8* Global_xo_ptr = &Global_xo_tune[0];

U8 Global_config1[5] = RF_GLOBAL_CONFIG_1;
U8* Global_conf_ptr = &Global_config1[0];

U8 Int_ctl_enable2[6]=RF_INT_CTL_ENABLE_2;
U8* Int_ctl_ptr = &Int_ctl_enable2[0];

U8 Frr_ctl_a_mode4[8] = RF_FRR_CTL_A_MODE_4;
U8* Frr_ctl_ptr = &Frr_ctl_a_mode4[0];

U8 Preamble_tx_length1[5] = RF_PREAMBLE_TX_LENGTH_1;
U8* Preamble_len_ptr= &Preamble_tx_length1[0];

U8 Preamble_config1[5] = RF_PREAMBLE_CONFIG_1;
U8* Preamble_conf_ptr= &Preamble_config1[0];

U8 Sync_config3[7] = RF_SYNC_CONFIG_3;
U8* Sync_conf_ptr= &Sync_config3[0];

U8 Pkt_config11[5] = RF_PKT_CONFIG1_1;
U8* Pkt_conf_ptr= &Pkt_config11[0];

U8 Pkt_field_1_config1[5] = RF_PKT_FIELD_1_CONFIG_1;
U8* Pkt_field_ptr= &Pkt_field_1_config1[0];

U8 Modem_mod_type12 [16] = RF_MODEM_MOD_TYPE_12;
U8* Modem_mod_ptr = &Modem_mod_type12[0];

U8 Modem_freq_dev01[5] = RF_MODEM_FREQ_DEV_0_1;
U8* Modem_freq_ptr= &Modem_freq_dev01[0];

U8 Modem_tx_ramp_delay8[12] = RF_MODEM_TX_RAMP_DELAY_8;
U8* Modem_tx_ptr= &Modem_tx_ramp_delay8[0];

U8 Modem_bcr_osr19[13] = RF_MODEM_BCR_OSR_1_9;
U8* Modem_bcr_ptr= &Modem_bcr_osr19[0];

U8 Modem_afc_gear7[11] = RF_MODEM_AFC_GEAR_7;
U8* Modem_afc_ptr = &Modem_afc_gear7[0];

U8 Modem_agc_control1[5] = RF_MODEM_AGC_CONTROL_1;
U8* Modem_contr_ptrb= &Modem_agc_control1[0];

U8 Modem_agc_window[13] = RF_MODEM_AGC_WINDOW_SIZE_9;
U8* Modem_window_ptr = &Modem_agc_window[0];

U8 Modem_ook[12] = RF_MODEM_OOK_CNT1_8;
U8* Modem_ook_ptr = &Modem_ook[0];

U8 Modem_rssi[5] = RF_MODEM_RSSI_COMP_1;
U8* Mosem_rssi_ptr = &Modem_rssi[0];

U8 Modem_clkgen[5] = RF_MODEM_CLKGEN_BAND_1;
U8* Modem_clkgen_ptr = &Modem_clkgen[0];

U8 Modem_chflt_rx1[16] = RF_MODEM_CHFLT_RX1_CHFLT_COE13_7_0_12;
U8* Modem_chflt_rx1_ptr = &Modem_chflt_rx1[0];

U8 Modem_chflt_rx12[16] = RF_MODEM_CHFLT_RX1_CHFLT_COE1_7_0_12;
U8* Modem_chflt_rx12_ptr = &Modem_chflt_rx12[0];

U8 Modem_chflt_rx2[16] = RF_MODEM_CHFLT_RX2_CHFLT_COE7_7_0_12;
U8* Modem_chflt_rx2_ptr = &Modem_chflt_rx2[0];

U8 Pa_mode[8] = RF_PA_MODE_4;
U8* Pa_mode_ptr = &Pa_mode[0];

U8 Synth_pfdcp[11] = RF_SYNTH_PFDCP_CPFF_7;
U8* Synth_pfdcp_ptr = &Synth_pfdcp[0];

U8 Freq_control[12] = RF_FREQ_CONTROL_INTE_8;
U8* Freq_contr_ptr = &Freq_control[0];


int main()
{
   // enter the default operating mode for this application (myModes.c).
   // Baud Rate = 115200
   //gModes_enter_async_uart_apb_mode();

   // Baud Rate = 9600
   gModes_enter_async_uart_rtc_mode();
   U8 var1;
   U8 var2;
   U8 var3;
   
  U8 data_trm = 0x23;
  U8 data_trm1 = 0x01;
  U8 data_rcv;
  U8* data_rcv_ptr;
  
  radio_HW_powerUp();
if(
  si446x_configuration_init(7,PWR_ptr) //power up parancs kiküldése
  |si446x_configuration_init(7,Gpio_ptr)
  |si446x_configuration_init(5,Global_xo_ptr)
  |si446x_configuration_init(5,Global_conf_ptr)
  |si446x_configuration_init(6,Int_ctl_ptr)
  |si446x_configuration_init(8,Frr_ctl_ptr)
  |si446x_configuration_init(5,Preamble_len_ptr)
  |si446x_configuration_init(5,Preamble_conf_ptr)
  |si446x_configuration_init(7,Sync_conf_ptr)
  |si446x_configuration_init(5,Pkt_conf_ptr)
  |si446x_configuration_init(5,Pkt_field_ptr)
  |si446x_configuration_init(16,Modem_mod_ptr)
  |si446x_configuration_init(5,Modem_freq_ptr)
  |si446x_configuration_init(12,Modem_tx_ptr)
  |si446x_configuration_init(13,Modem_bcr_ptr)
  |si446x_configuration_init(11,Modem_afc_ptr)
  |si446x_configuration_init(5,Modem_contr_ptrb)
  |si446x_configuration_init(13,Modem_window_ptr)
  |si446x_configuration_init(12,Modem_ook_ptr)
  |si446x_configuration_init(5,Mosem_rssi_ptr)
  |si446x_configuration_init(5,Modem_clkgen_ptr)
  |si446x_configuration_init(16,Modem_chflt_rx1_ptr)
  |si446x_configuration_init(16,Modem_chflt_rx12_ptr)
  |si446x_configuration_init(16,Modem_chflt_rx2_ptr)
  |si446x_configuration_init(8,Pa_mode_ptr)
  |si446x_configuration_init(11,Synth_pfdcp_ptr)
  |si446x_configuration_init(12,Freq_contr_ptr)
)
{
while(1){
	SI32_PBSTD_A_write_pins_high(SI32_PBSTD_1,0x010); //was any fault during the initialization process
	//SI32_PBSTD_A_toggle_pins(SI32_PBSTD_1,0x010);
	for(int wt = 0;wt<200000; wt++);
	SI32_PBSTD_A_write_pins_low(SI32_PBSTD_1,0x010);
	for(int wt = 0;wt<200000; wt++);
	}
}
else
	{
		SI32_PBSTD_A_write_pins_low(SI32_PBSTD_1,0x010); //all init command completed succesfully LED1.4 ON
		for(int wt = 0;wt<800000; wt++);
		SI32_PBSTD_A_write_pins_high(SI32_PBSTD_1,0x010);
	}

while(SI32_PBSTD_A_read_pin(SI32_PBSTD_1,6));
/*
	var1 = radio_comm_SendCmdGetResp(4,Get_itstat_ptr,9,partarray_ptr);
    myUART0_send_char(var1); //FF-et küld ki elvileg
    //  myUART0_send_char(0x33);
    for(U8 k = 0;k<9;k++){
    myUART0_send_char(*partarray_ptr++); //végigszalad az előbb beolvasott pointeren
    }
    //lassito ciklus
    for(int i = 0;i<16000;i++);*/

    var1 = radio_comm_SendCmdGetResp(1,getpartinf_ptr,8,partarray_ptr);
    myUART0_send_char(var1); //FF-et küld ki elvileg
    //  myUART0_send_char(0x33);
    for(U8 k = 0;k<8;k++){
    myUART0_send_char(*partarray_ptr++); //végigszalad az előbb beolvasott pointeren
    }
    //lassito ciklus
    for(int i = 0;i<16000;i++);

   while (1)
   {
	   if(!SI32_PBSTD_A_read_pin(SI32_PBSTD_1,4)){

		   	SI32_SPI_B_clear_nss (SI32_SPI_1);
		   	// Wait for NSS pin low
		   	while(SI32_SPI_B_get_nss_pin(SI32_SPI_1));

		   	//flush tx, rx fifo
		   	SI32_SPI_B_flush_rx_fifo(SI32_SPI_1);
		   	SI32_SPI_B_flush_tx_fifo(SI32_SPI_1);

		   	//clear SPI interrupts
		   	SI32_SPI_B_clear_all_interrupts(SI32_SPI_1);


		//   	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x44);

		   	//waiting for CTS after chip select..
		/*   	do{
		   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x44);
		   		//SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1) ;
		   	//	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x00);
		   	}while(SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1) != 0xFF);*/

		   	//SPI write power up API
		   	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x01);

	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x44);

	   		/*
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x00);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x01);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x8C);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xBA);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x80);*/


		   	//safety waiting...
		   	for(int i = 0;i<2000;i++);

			SI32_SPI_B_flush_rx_fifo(SI32_SPI_1);

			//write GET_STATUS api
		 //  	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, data_trm);

		   	data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   			   		myUART0_send_char(0x50);
		   			    	myUART0_send_char(data_rcv);

	/*	   	do{
		   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x44);
		   		//SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1) ;
		   		//SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x00);
		   	}while(SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1) != 0xFF);*/


		   	//write zeros to TX for read something (for example status...) from rx later
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);
	   		SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0xFF);

	   		//???????? from example code
		   	while(0 == SI32_SPI_B_is_shift_register_empty_interrupt_pending(SI32_SPI_1));
		   	while (SI32_SPI_B_get_rx_fifo_count(SI32_SPI_1) < 1);


		   	data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   			   		myUART0_send_char(0x50);
		   			    	myUART0_send_char(data_rcv);


		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+1);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+2);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+3);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+4);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+5);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+6);
		   		myUART0_send_char(data_rcv);

		   		data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   		myUART0_send_char(0x41+7);
		   		myUART0_send_char(data_rcv);


	/*	   	// Read the response from the slave, shift out zeros
		   	   SI32_SPI_B_clear_all_interrupts(SI32_SPI_1);
		   	   SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1, 0x00);
		   	   while (SI32_SPI_B_get_rx_fifo_count(SI32_SPI_1) < 1);
		   	   data_rcv = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);

		   	myUART0_send_char(0x52);
		   	myUART0_send_char(data_rcv);
		   	myUART0_send_char(0xA);
		   	myUART0_send_char(0xD);
*/

		   	   // Generate NSS Rising Edge
		   	   SI32_SPI_B_set_nss (SI32_SPI_1);
		   	   // Wait for NSS high


		   	   while(0 == SI32_SPI_B_get_nss_pin(SI32_SPI_1));

		   	for(int i = 0;i<400000;i++);
	  	 	}

	   else if(!SI32_PBSTD_A_read_pin(SI32_PBSTD_1,5)){

		   	SI32_SPI_B_clear_nss (SI32_SPI_1);
		   	// Wait for NSS pin low
		   	while(SI32_SPI_B_get_nss_pin(SI32_SPI_1));

		  	//flush tx, rx fifo
		   	SI32_SPI_B_flush_rx_fifo(SI32_SPI_1);
		   	SI32_SPI_B_flush_tx_fifo(SI32_SPI_1);

		   	//clear SPI interrupts
		   	SI32_SPI_B_clear_all_interrupts(SI32_SPI_1);

		   	for(U8 j = 0;j<10;j++){
		   	data_trm = getNumber_fromUART();
		   	myUART0_send_char(data_trm);
		   	SI32_SPI_B_write_tx_fifo_u8(SI32_SPI_1,data_trm);
		   	var2 = SI32_SPI_B_read_rx_fifo_u8(SI32_SPI_1);
		   for(U8 i=0;i<255;i++);
		   	myUART0_send_char(var2);
		   	}

		   	//safety waiting...
		   	for(int i = 0;i<2000;i++);

			SI32_SPI_B_flush_rx_fifo(SI32_SPI_1);

		   	   // Generate NSS Rising Edge
		   	   SI32_SPI_B_set_nss (SI32_SPI_1);
		   	   // Wait for NSS high
		   	   while(0 == SI32_SPI_B_get_nss_pin(SI32_SPI_1));

		   	for(int i = 0;i<200000;i++);
	  	 	  }


	   else if(!SI32_PBSTD_A_read_pin(SI32_PBSTD_1,6)){

	   }
	   else if(!SI32_PBSTD_A_read_pin(SI32_PBSTD_1,7)){
		   DemoApp_Pollhandler();
	   }
	   else{

	  	 }
	    }
   }

//-eof--------------------------------------------------------------------------
